<?php

abstract class OrganizationAbstract extends Node
{

    protected $fiscal_id;
    protected $name;
    protected $commercial_name;
    protected $parent;
    protected $logo;
    protected $css;
    protected $extension;
    protected $editable = false;
    protected $form_fiscal_id;
    protected $form_name;
    protected $form_commercial_name;

    function setEditable( $edit )
    {
        $this->editable = $edit;
    }

    function setFormData( $fiscal_id, $name, $commercial_name )
    {
        $this->form_fiscal_id = $fiscal_id;
        $this->form_name = $name;
        $this->form_commercial_name = $commercial_name;

        if ( empty( $name ) ) $this->errors[ ] = 'name';

        if ( empty( $fiscal_id ) || !self::control_cif( $fiscal_id ) )
                $this->errors[ ] = 'fiscal_id';

        if ( empty( $commercial_name ) ) $this->errors[ ] = 'commercial_name';
    }

    function handleGET( Event $e )
    {
        if ( $e->tag == 'editar' ) {
            $this->_setEditable( true );
            $this->_setFormData( $this->fiscal_id, $this->name,
                    $this->commercial_name );
            $e->cancelBubble();
        } else if ( $e->tag == 'cerrar' ) {
            $this->_setEditable( false );
            $this->_setFormData( '', '', '' );
            $e->cancelBubble();
        }
    }

    function handlePOST( Event $e )
    {
        if ( $e->tag == 'editar' ) {
            if ( $_POST[ 'quote_update' ] == '1' ) {

                $_POST[ 'name' ] = trim( preg_replace( '/\s+?/', ' ',
                                $_POST[ 'name' ] ) );
                $_POST[ 'commercial_name' ] = trim( preg_replace( '/\s+?/', ' ',
                                $_POST[ 'commercial_name' ] ) );
                $_POST[ 'fiscal_id' ] = strtr( $_POST[ 'fiscal_id' ],
                        array( '.' => '', '-' => '', ' ' => '' ) );

                $this->_setFormData( $_POST[ 'fiscal_id' ], $_POST[ 'name' ],
                        $_POST[ 'commercial_name' ] );
            }

            $this->_setEditable( false );
            $e->cancelBubble();
        } else if ( $e->tag == 'establecer_logo' ) {

            if ( !empty( $_FILES[ 'logo' ] ) ) {
                $parts = explode( '.', $_FILES[ 'logo' ][ 'name' ] );
                $this->extension = strtoupper( end( $parts ) );

                if ( !in_array( $this->extension,
                                array( 'PNG', 'JPEG', 'JPG', 'GIF' ) ) )
                        $this->errors[ ] = 'extension';
            }
            else $this->errors[ ] = 'extension';
        }
    }

    function doPOST( Event $e )
    {
        if ( $e->tag == 'eliminar' ) $this->delete();

        else if ( $e->tag == 'editar' ) {
            $this->update();
            $this->_setEditable( false );
            $e->cancelBubble();
        } else if ( $e->tag == 'establecer_logo' ) {
            $this->setLogo();
            $e->cancelBubble();
        } else if ( $e->tag == 'eliminar_logo' ) {
            $this->unsetLogo();
            $e->cancelBubble();
        } else if ( $e->tag == 'establecer_css' ) {
            $this->setCSS();
            $e->cancelBubble();
        } else if ( $e->tag == 'eliminar_css' ) {
            $this->unsetCSS();
            $e->cancelBubble();
        }
    }

    static function control_cif( $cif )
    {
        $cif = ( string ) $cif;

        if ( preg_match( '/^([A-HJ-NSUV]\d{8})|([ANPQRSW]\d{7}[A-J])$/', $cif ) ) {

            $clave = $cif[ 0 ];
            $control = substr( $cif, -1, 1 );
            $numero = substr( $cif, 1 );

            $suma = 0;
            $par = false;
            for ( $i = 0; $i < 7; $i++ ) {
                if ( $par ) $suma+=( int ) $numero[ $i ];
                else {
                    $mul = 2 * (( int ) $numero[ $i ]);
                    if ( $mul >= 10 ) $mul-=9;
                    $suma+=$mul;
                }
                $par = !$par;
            }
            $suma = ( string ) $suma;

            $total = 10 - (( int ) substr( $suma, -1, 1 ));
            if ( $total == 10 ) $total = 0;

            if ( is_numeric( $control ) ) return ((( int ) $control) == $total);
            else {
                $letras = array( 'J', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I' );
                return ($letras[ $total ] == $control);
            }
        }

        return false;
    }

    abstract function update();

    abstract function delete();

    function setLogo()
    {
        move_uploaded_file( $_FILES[ 'logo' ][ 'tmp_name' ],
                'archivos' . DIRECTORY_SEPARATOR . 'logos' . DIRECTORY_SEPARATOR . $this->id . '.' . $this->extension );


        $sql = 'UPDATE enterprises SET logo=\'' . $this->extension . '\' WHERE id=' . $this->id;
        MyLink::$default->execute( $sql );
    }

    function unsetLogo()
    {
        $sql = 'UPDATE enterprises SET logo=\'\' WHERE id=' . $this->id;
        MyLink::$default->execute( $sql );
    }

    function setCSS()
    {
        move_uploaded_file( $_FILES[ 'css' ][ 'tmp_name' ],
                'archivos' . DIRECTORY_SEPARATOR . 'css' . DIRECTORY_SEPARATOR . $this->id . '.css' );


        $sql = 'UPDATE enterprises SET css=\'S\' WHERE id=' . $this->id;
        MyLink::$default->execute( $sql );
    }

    function unsetCSS()
    {
        $sql = 'UPDATE enterprises SET css=\'N\' WHERE id=' . $this->id;
        MyLink::$default->execute( $sql );
    }

}

?>
